var networkProject={
		treeNodeId:"",
    init:function(){
        var self=this;
        self.shadeDiv = $("#shade-div");
        self.bindEvents();
        self.bindEvents1();
        //self.loadTree()
        self.personnelSelection();
    },
    bindEvents:function(){
        var self=this;
//        全选
        $.checkAll("inputItem1")
        $.checkAll("inputItem2")
        $("#findGroup").textClean()
        //        选择用户
       /* $("#newAdds").click(function(){
        	
        	
        	
          	var treeSetting = {
        		    check:{enable: true,chkStyle: "checkbox",chkboxType: { "Y": "s", "N": "ps"}},
        		    data:{simpleData: {enable: true}},
        			async: {
        				enable: true,
        				url:"../userconfig/group_management!getUserTT.xhtml",
        				autoParam:["id"]
        			}
        	};
        	//加载部门数据
          	networkProject.ajaxPOST("../userconfig/group_management!getDeptUserTree.xhtml","",function(data){
        		$.fn.zTree.init($("#treeDemo"), treeSetting, data);
        		var treeDemo=$.fn.zTree.getZTreeObj("treeDemo");
        	});
        	
        	
            self.shadeDiv.show();
            $('#addUser').setPosition().show();

        })*/
        
        $("#newAdds").click(function(){
          		networkProject.treeNodeId="";
    		$("#table_left").html("");
    		$("#table_right").html("");
     		 self.shadeDiv.show();
            $('#personnelSelection').setPosition().show();
	       	var treeSetting = {
	       		    data:{simpleData: {enable: true}},
	       			async: {
	       				enable: true,
	       				url:ctx+"/userconfig/group_management!getUserTT.xhtml",
	       				autoParam:["id"]
	       			},callback: {
	    				onClick:self.treenodeClick
	    			}
	       	};
	       	//加载部门数据 
	       	networkProject.ajaxPOST(ctx+"/userconfig/group_management!getDeptUserTree.xhtml","",function(data){
	       		$.fn.zTree.init($("#treeDemo"), treeSetting, data);
	       		var treeDemo=$.fn.zTree.getZTreeObj("treeDemo");
	       	});
       });
        
        //  确定
        $("#btnOk").click(function(){
           
        	var deptChecked=$.fn.zTree.getZTreeObj("treeDemo").getCheckedNodes();
        	var deptIds="";//部门菜单选中
        	for(var i=0;i<deptChecked.length;i++){
        		if(i==0){
        			deptIds+=deptChecked[i].id;
        		}else{
        			deptIds+=","+deptChecked[i].id;
        		}
        	}
        	
        	   var groupid=$("#groupid").val();
          	$.ajax({
        		type : "POST",
        		url : "../userconfig/group_management!addUserInfo.xhtml",
        	data : {groupid:groupid,userIds:deptIds},
        		success : function(data) {
        			 self.shadeDiv.hide();
        			if(data=='true'){
        			//alert("修改成功！");
        			artDialog({icon: 'succeed',title: '提示',lock: true,opacity: 0.2,zIndex:8888,content:"添加成功！",button:[{name: '确定'}]});
        			//common.refreshPage();
        			 // $('#addUser').setPosition().hide();
        			window.location.reload();
        			}else{
        				//alert(data);	
        				artDialog({icon: 'warning',title: '提示',lock: true,opacity: 0.2,zIndex:8888,content:data,button:[{name: '确定'}]});
        			}
        		}
        	});
        	
        })
        
    $("#pushData").click(function(){
    	 
    	var deptIds="";//部门菜单选中
    	  var userid=new Array() ;
    	
    	  var trAll=$(".selectionRight table tr");
          trAll.each(function(){
        	  userid.push($(this).attr("id"));
        	 // userIdsTxt.push($(this).find(".memberName>span em").text());
        	 
          });
          deptIds=userid.join(",");
        //var countDep=  deptIds.split(",");
         
         // alert(deptIds);
          
          
   	   var groupid=$("#groupid").val();
     	$.ajax({
   		type : "POST",
   		url : "../userconfig/group_management!addUserInfo.xhtml",
   	    data : {groupid:groupid,userIds:deptIds},
   		success : function(data) {
   			 self.shadeDiv.hide();
   			if(data=='true'){
   			//alert("修改成功！");
   			artDialog({icon: 'succeed',title: '提示',lock: true,opacity: 0.2,zIndex:8888,content:"添加成功！",button:[{name: '确定'}]});
   			//common.refreshPage();
   			 // $('#addUser').setPosition().hide();
   			window.location.reload();
   			}else{
   				//alert(data);	
   				artDialog({icon: 'warning',title: '提示',lock: true,opacity: 0.2,zIndex:8888,content:data,button:[{name: '确定'}]});
   			}
   		}
   	});
        
    	$('#personnelSelection').hide();
      
   })
        $("#searchDep").textClean();
        //        搜索
        $("#searchName input").textClean()
        $("#searchName input").focus(function(){
            $(this).siblings("span").show();
        })
        $("#searchName input").blur(function(){
            if($.trim($(this).val()) == ""){
                $(this).siblings("span").hide();
            };

        })
        $(".removeSearch").click(function(){
            $(this).siblings("input").val("")
        })
        $('.searchName').keydown(function(e){
            if(e.keyCode==13){
                $(this).click(); //处理事件
            }
        });
//        重命名
        $(".reName").click(function(){
            self.shadeDiv.show();
            $('#reName').setPosition().show();

        })
        //重命名按钮
           $("#cmmBtn").click(function(){
          //  self.shadeDiv.show();
            //$('#reName').setPosition().show();
        	   var groupid=$("#groupid").val();
               var groupname=$("#groupname").val();
               var description=$("#description").val(); 
        	   	$.ajax({
            		type : "POST",
            		url : "../userconfig/group_management!update.xhtml",
            	data : {groupid:groupid,groupname:groupname,description:description},
            		success : function(data) {
            			 self.shadeDiv.hide();
            			if(data=='true'){
            			//alert("修改成功！");
            			artDialog({icon: 'succeed',title: '提示',lock: true,opacity: 0.2,zIndex:8888,content:"修改成功！",button:[{name: '确定'}]});
            			common.refreshPage();
            			  $('#editDX').setPosition().hide();
            			}else{
            				//alert(data);	
            				artDialog({icon: 'warning',title: '提示',lock: true,opacity: 0.2,zIndex:8888,content:data,button:[{name: '确定'}]});
            			}
            		}
            	});
        	   

        })

//        退出该群
        $(".signOut").click(function(){
            self.shadeDiv.show();
            $('#warning8').setPosition().show().find("p.center").html("确定要把选中成员当前群吗？");

        });
//        取消管理员
/*        $(".cancel").click(function(){
            self.shadeDiv.show();
            $('#warning').setPosition().show().find("p.center").html("确定要把该成员取消群组管理员吗？");

        });*/
//        移除该群
        $(".remove").click(function(){
            self.shadeDiv.show();
            $('#warning').setPosition().show().find("p.center").html("确定要把该成员当前群吗？");
           var ss= $(this).attr("title");
            $("#upId").val(ss);

        });
//        设置管理员
        $(".setUp").click(function(){
            self.shadeDiv.show();
            $('#warning').setPosition().show().find("p.center").html("确定要把该成员设置群组管理员吗？");

        });




    },treenodeClick:function(event, treeId, treeNode, clickFlag){
    	
    	var self=this;
    	var data=$("#personnelSelection_text").val();
  	    var postData={
              "data":data
        };
  	  networkProject.treeNodeId=treeNode.id;
    	//加载部门数据
  	  networkProject.loadLeftData(treeNode.id,postData);
    	
    }, personnelSelection:function(){
    	var self=this;
//      阻止复选框冒泡事件
      $(".selectionBot>div table tr input").live("dblclick",function(event){
          event.stopPropagation()
      })

      //        单击成员选中
      $(".selectionBot>div table tr").live("click",function () {
          var $this=$(this);
          $this.addClass("cur").siblings("tr").removeClass("cur")
      });
//      双击移动成员
      $(".selectionBot>div table tr").live("dblclick",function () {
              //双击事件的执行代码
          $(this).removeClass("cur")
          $(this).parents("div.mobile").siblings("div.mobile").find("table").append($(this));
      })
      
//      右移单个成员
      $("button.singleRight").live("click",function(){
          	var tr=$(".selectionLeft table tr.cur");
          	$(".selectionRight table").append(tr);          	
            tr.removeClass("cur");
      });
//      左移单个成员
      $("button.singleLeft").live("click",function(){
          	var tr=$(".selectionRight table tr.cur");
          	$(".selectionLeft table").append(tr);
            tr.removeClass("cur");
      });
//      右移全部成员
      $("button.allRight").live("click",function(){
          var trAll=$(".selectionLeft table tr");
          $(".selectionRight table").append(trAll);
      });
//      左移全部成员
      $("button.allLeft").live("click",function(){
          var trAll=$(".selectionRight table tr");
          $(".selectionLeft table").append(trAll);
      });
//      鼠标滑过姓名显示账号
      $(".selectionBot>div table tr div.memberName").live('hover',function(event){
          if(event.type=='mouseenter'){
              $(this).find("span").show();
          }else{
              $(this).find("span").hide();
          }
      });
      $("#pushData").click(function(){
    	 
    		$("#userIdsTxt").html("");
        	$("#userIds").val("");
    	  
    	  var userid=new Array() ;
    	  var userIdsTxt=new Array() ;
    	  var trAll=$(".selectionRight table tr");
          trAll.each(function(){
        	  userid.push($(this).attr("id"));
        	  userIdsTxt.push($(this).find(".memberName>span em").text());
        	  
        	  //alert($(this).attr("id")+$(this).find(".memberName>span em").text());
          });
          //alert(userid.join(",")+userIdsTxt.join(","));
          

  		$("#userIdsTxt").html(userIdsTxt.join(","));
      	$("#userIds").val(userid.join(",")); 
        /* var postData={
        	"data":userid.join(",")
         };*/
        
        //加载部门数据
        // networkProject.ajaxPOST(ctx+"/userconfig/virtual_group!saveUser.xhtml?fteam.id=5",postData,function(data){
       	//	common.refreshPage();
      // 	});
      	$('#personnelSelection').hide();
      });
    	
      $("#personnelSelection_search").click(function(){
    	  var data=$("#personnelSelection_text").val();
    	  var postData={
                  "data":data
          };
    	  self.loadLeftData(networkProject.treeNodeId,postData);
      });
      $("#personnelSelection_clear").click(function(){
    	  
    	  if(networkProject.treeNodeId!=""){
    		  $("#personnelSelection_text").val("");
        	  var postData={
                      "data":""
              };
        	  self.loadLeftData(networkProject.treeNodeId,postData);
    	  }
      });
    },loadLeftData:function(treeId,postData,pageNumber){
    	if(pageNumber==null){
    		pageNumber=1;
    	}
    	
    	//加载部门数据
    	networkProject.ajaxPOST(ctx+"/userconfig/group_management!getUserListByDept.xhtml?pager.pageNumber="+pageNumber+"&id="+treeId,postData,function(re){
     		data=re.list;
     		$("#table_left").html("");
     		var htm_left="";
     		for(var i=0;i<data.length;i++){
 				htm_left+="<tr id=\""+data[i].id+"\">" +
			"<td style=\"width:33%\"><div class=\"memberName\">"+data[i].name+"<span class=\"memberNum hide\"><em>"+data[i].name+"</em></span></div></td>" +
			"<td style=\"width:33%\">"+data[i].deptName+"</td>" +
			"<td style=\"width:30%\">"+data[i].local+"</td>" +
			"</tr>";
     		}
     		$("#table_left").html(htm_left);
     		$(".paginationLeft").unbind();
     		$('.paginationLeft').jqPagination({
                link_string : ''+re.pageNumber,
                current_page: re.pageNumber, //设置当前页 默认为1
                max_page : re.pageCount, //设置最大页 默认为1
                page_string : '当前第'+re.pageNumber+'页,共'+re.pageCount+'页',
                paged : function(page) {
                	networkProject.loadLeftData(treeId,postData,page);
                }
            });
     	});
    },contains:function(string,substr,isIgnoreCase){
	    if(isIgnoreCase){
		    string=string.toLowerCase();
		    substr=substr.toLowerCase();
	    }
	    var startChar=substr.substring(0,1);
	    var strLen=substr.length;
	    for(var j=0;j<string.length-strLen+1;j++){
		    if(string.charAt(j)==startChar){//如果匹配起始字符,开始查找
			    if(string.substring(j,j+strLen)==substr) {//如果从j开始的字符与str匹配，那ok
			    	return true;
			    }  
		    }
	    }
	    return false;
    },
    bindEvents1:function(){
        var self=this;
//        分页
//        左侧表格
        $('.paginationLeft').jqPagination({
            link_string : '/?page={page_number}',
            current_page: 5, //设置当前页 默认为1
            max_page : 40, //设置最大页 默认为1
            page_string : '当前第{current_page}页,共{max_page}页',
            paged : function(page) {
                //回发事件。。。
            }
        });
//        右侧表格
        $('.paginationRight').jqPagination({
            link_string : '/?page={page_number}',
            current_page: 5, //设置当前页 默认为1
            max_page : 50, //设置最大页 默认为1
            page_string : '当前第{current_page}页,共{max_page}页',
            paged : function(page) {
                //回发事件。。。
            }
        });

//        新增账户
        $("#newAccount").click(function(){
            self.shadeDiv.show();
            $('#personnelSelection').setPosition().show();
        })


    },
    loadTree:function(){
      //  $.fn.zTree.init($("#treeDemo"), setting1, zNode1);
      //  $.fn.zTree.init($("#treeDemo1"), setting1, zNode1);
    },
    ajaxGET:function (url, data, fn,beforeSendFn) {
        var beforeSendFn = beforeSendFn || function(){};
        $.ajax({type:"GET", dataType:'json', context:this, url:url, data:data, success:fn, beforeSend:beforeSendFn});
    },
    ajaxPOST:function (url, data, fn,beforeSendFn) {
        var beforeSendFn = beforeSendFn || function(){};
        $.ajax({type:"POST", dataType:'json', context:this, url:url, data:data, success:fn, beforeSend:beforeSendFn});
    }
}
$(function(){
    networkProject.init()
})

var zid="";
var ztype="";
var zuserid="";
function qy(id,type,userid){
	zid=id;
	ztype=type;
	zuserid=userid ;
	if(type==1){
	 $('#warning3').setPosition().show().find("p.center").html("确定取消管理员？");
	}else if(type==0){
	  $('#warning3').setPosition().show().find("p.center").html("确定设置管理员？");
	}

	
}

function deletenp(){
	var  upId= $("#upId").val();
	var upIds=upId.split("_");
	var groupid=upIds[0];
	var userid=upIds[1];
	 $('#warning3').setPosition().hide();
	$.ajax({
		type : "POST",
		url : "../userconfig/group_management!delete.xhtml",
		data : "groupid="+groupid+"&userid="+userid,
		beforeSend : function() {
		},
		success : function(data) {
			//alert("删除成功");
			artDialog({icon: 'succeed',title: '提示',lock: true,opacity: 0.2,zIndex:8888,content:"删除成功！",button:[{name: '确定'}]});
			common.refreshPage();
		}
	});
	
	
}

function deleteALL(){
	
	 var str="";
     $("input[name='userCheck']:checkbox").each(function(){ 
         if($(this).attr("checked")){
             str += $(this).val()+","
         }
     })
   
    var users= str.split(",");
    // alert(users.length);
  if(users.length>1){
	 $('#warning8').setPosition().hide();
	 var groupid=$("#groupid").val();
	$.ajax({
		type : "POST",
		url : "../userconfig/group_management!deleteALL.xhtml",
		data : "groupid="+groupid+"&userIds="+str,
		beforeSend : function() {
		},
		success : function(data) {
			//alert("删除成功");
			artDialog({icon: 'succeed',title: '提示',lock: true,opacity: 0.2,zIndex:8888,content:"删除成功！",button:[{name: '确定'}]});
			common.refreshPage();
		}
	});
  }else{
	  artDialog({icon: 'warning',title: '提示',lock: true,opacity: 0.2,zIndex:8888,content:"请选中要删除的用户！",button:[{name: '确定'}]});
  }
	
}


function qyok(){
	
	 $('#warning').setPosition().hide();
	$.ajax({
		type : "POST",
		url : "../userconfig/group_management!updateAdmin.xhtml",
		data : "groupid="+zid+"&utype="+ztype+"&userid="+zuserid,
		beforeSend : function() {
		},
		success : function(data) {
			
		    // $('#warning').setPosition().show().find("p.center").html(data);
			if(ztype==0){
				//alert("设置成功");
				artDialog({icon: 'succeed',title: '提示',lock: true,opacity: 0.2,zIndex:8888,content:"设置成功！",button:[{name: '确定'}]});
				$("#ss"+zuserid).html("已启用管理员");
				$("#ss"+zuserid).attr("href","javascript:qy("+zuserid+",0)");
				common.refreshPage();
			}else if(ztype==1){
				//alert("禁用成功");
				artDialog({icon: 'succeed',title: '提示',lock: true,opacity: 0.2,zIndex:8888,content:"禁用成功！",button:[{name: '确定'}]});
				$("#ss"+zid).html("已禁用管理员");
				$("#ss"+zid).attr("href","javascript:qy("+zid+",1)");
				common.refreshPage();
			}
		}
	});
	
	
}